package com.example.children.controller;

import com.example.children.Exception.LoginException;
//import com.example.children.Service.LoginService;
import com.example.children.Service.LoginService;
import com.example.children.Util.*;
//import com.example.children.Util.MD5Util;
import com.example.children.annotation.AllowGuestAccess;
import com.example.children.Model.User;
import com.example.children.Model.ResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;


@RestController
//@CrossOrigin(origins = {"http://localhost:8080","http://localhost:63342"}) // 允许该源
@RequestMapping("/Login")  //类名
public class Login  extends BaseServlet{

    // 通过依赖注入注入 LoginService
    @Autowired
    private LoginService loginService;///


    @PostMapping("/login") //方法名
    public ResponseModel<User> login(@RequestBody User user) throws NoSuchAlgorithmException {
        System.out.println("进入login方法："+user.getUsername());
        Integer code= Tools.Success;
        String msg= tool.success;
        String accessToken =null;
        String refreshToken =null;
        Map<String, String> toKenMap =new HashMap();
        String password= MD5Util.encrypt(user.getPassword());
        user.setPassword(password);
        user=loginService.validateCredentials(user);
        if (user!=null) {
            System.out.println("Yes："+user.getRole());
//        生成jwt
            accessToken= JwtUtil.generateToken(user.getId(),1000*5,"user");
            refreshToken= JwtUtil.generateToken(user.getId(),1000*10,"user");
            toKenMap.put("accessToken",accessToken);
            toKenMap.put("refreshToken",refreshToken);
        }
        else{
            System.out.println("No");
            throw new LoginException("用户名或密码错误");
        }
        return new ResponseModel<>(user,code,msg,toKenMap);//响应给前端的对象
    }

    @PostMapping("/getInvitationCode") //方法名
    public ResponseModel<String> invitationCode(@RequestBody User user) throws NoSuchAlgorithmException {
        System.out.println("进入getInvitationCode方法："+user.getUsername());
        Integer code= Tools.Success;
        String msg= tool.success;
        String invitationCode= UUIDGenerator.generateNumericUUID(4);
        user.setInvitationCode(invitationCode);
        loginService.saveInvitationCode(user);
        return new ResponseModel<>(invitationCode,code,msg,null);//响应给前端的对象
    }



    @AllowGuestAccess
    @PostMapping("/guestLogin")  //游客登录
    public ResponseModel<Map<String, String>> guestLogin(){
        System.out.println("进入guestLogin方法：游客登录");
        Integer code= Tools.Success;
        String msg= tool.success;
        String accessToken =null;
        String refreshToken =null;
        Map<String, String> toKenMap =new HashMap();
            System.out.println("Yes");
            accessToken= JwtUtil.generateToken(null,1000*5,"guest");
            refreshToken= JwtUtil.generateToken(null,1000*10,"guest");
            toKenMap.put("accessToken",accessToken);
            toKenMap.put("refreshToken",refreshToken);
        return new ResponseModel<>(code,msg,toKenMap);//响应给前端的对象
    }







}

